package com.argrace.smart.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.sdk.android.openaccount.OpenAccountSDK;
import com.alibaba.sdk.android.openaccount.callback.LoginCallback;
import com.alibaba.sdk.android.openaccount.model.OpenAccountSession;
import com.alibaba.sdk.android.openaccount.ui.OpenAccountUIService;
import com.aliyun.alink.business.login.AlinkLoginBusiness;
import com.aliyun.alink.business.login.IAlinkLoginCallback;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;

/**
 * Created by argrace_dev_1 on 2018/1/29.
 */

public class LoginActivity extends BaseActivity {
    private SharedPreferences spLogin;
    private SharedPreferences.Editor editor;
    OpenAccountUIService openAccountService = OpenAccountSDK.getService(OpenAccountUIService.class);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        spLogin = getSharedPreferences(ConstantConfigure.SPLOGIN_NAME, MODE_PRIVATE);
        init();
    }

    private void init() {
//        if (AlinkLoginBusiness.getInstance().isLogin()) {
//            Log.i("info", "用户已登陆" + AlinkLoginBusiness.getInstance().getSessionID());
//            Toast.makeText(this, "账号已登录", Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(LoginActivity.this, MainActivity.class));
//
//            finish();
//        } else {
//            Log.i("info", "用户未登录，调用百川登陆登陆");
//            openAccountService.showLogin(LoginActivity.this, new LoginCallback() {
//                @Override
//                public void onSuccess(OpenAccountSession openAccountSession) {
//                    editor = spLogin.edit();
//                    editor.putString("token", openAccountSession.getAuthorizationCode());
//                    editor.putString("id", openAccountSession.getUserId());
//                    editor.putString("otherinfo", openAccountSession.getOtherInfo().toString());
//                    editor.putString("mobile", openAccountSession.getUser().mobile);
//                    editor.commit();
//                    Log.i("info", "登陆成功" + openAccountSession.getAuthorizationCode());
//                }
//
//                @Override
//                public void onFailure(int i, String s) {
//                    Log.i("info", "登陆失败");
//                }
//            });
//        }
        if (AlinkLoginBusiness.getInstance().isLogin()) {
            Log.i("info", "账号已登陆,userid=" + AlinkLoginBusiness.getInstance().getUserID() + "---seeionid" + AlinkLoginBusiness.getInstance().getSessionID() + "--username" + AlinkLoginBusiness.getInstance().getNickName());
            Toast.makeText(this, "账号已登录", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        } else {
            Log.i("info", "账号未登录过，跳转到注册界面");
            AlinkLoginBusiness.getInstance().login(this, new IAlinkLoginCallback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onFailure(int i, String s) {

                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }
}
